package com.hxkcloud.patient.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.activitys.ChartDetailActivity;
import com.hxkcloud.patient.beans.HospitalStatusBean;
import com.hxkcloud.patient.beans.LineListBean;
import com.hxkcloud.patient.beans.PiontListBean;
import com.hxkcloud.patient.beans.PointBean;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.util.ChartUtils;
import lecho.lib.hellocharts.view.LineChartView;

/**
 * Created by ray_lei on 2016/7/6.
 */
public class ChartAdapter extends RecyclerView.Adapter<ViewHolder>{


    private Context mContext;
    private List<LineListBean> datas;
    private RecyclerView recyclerview;
    private LayoutInflater mInflater;

    private ResultSelectedListener listener;

    List<HospitalStatusBean> beans;

    String patientId;

    public ChartAdapter(Context mContext, List<LineListBean> datas,
                        RecyclerView recyclerview, ResultSelectedListener listener,String patientId) {
        this.mContext = mContext;
        this.datas = datas;
        this.recyclerview = recyclerview;
        this.listener = listener;
        this.patientId = patientId;
        mInflater = LayoutInflater.from(mContext);
    }

    public void setData(List<LineListBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void clearData() {
        datas.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {


        Holder mHolder = (Holder) holder;
        mHolder.titleTV.setText(datas.get(position).getName());
        mHolder.dateTV.setText(datas.get(position).getDate());

        generateData(datas.get(position).getMinValue(), datas.get(position).getMaxValue(), datas.get(position).getLineList(), mHolder.chartLayout);
        mHolder.chartLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,ChartDetailActivity.class);
                intent.putExtra("patientId",patientId);
                intent.putExtra("kindId",datas.get(position).getKindId());
                intent.putExtra("name",datas.get(position).getName());
                intent.putExtra("min",datas.get(position).getMinValue());
                intent.putExtra("max",datas.get(position).getMaxValue());
                if(datas.get(position).getLineList()!=null){
                    for(int i = 0;i<datas.get(position).getLineList().size(); i++){
                        intent.putExtra("text"+i,datas.get(position).getLineList().get(i).getName());
                    }
                }
                mContext.startActivity(intent);

            }
        });


    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        View v = mInflater.inflate(R.layout.item_chart_layout, null);
        ViewHolder holer = new Holder(v);

        return holer;
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    /*
     * Holder
     */
    public class Holder extends ViewHolder {

          ImageView iconIV;
          TextView titleTV;
          TextView dateTV;
          LineChartView chartLayout;


        public Holder(View view) {
            super(view);
            iconIV = (ImageView) view.findViewById(R.id.item_chart_icon);
            titleTV = (TextView) view.findViewById(R.id.item_chart_title);
            dateTV = (TextView) view.findViewById(R.id.item_chart_date);
            chartLayout = (LineChartView) view.findViewById(R.id.item_chart);


        }
    }

    public interface ResultSelectedListener {
        void onSelected(String type);
    }

    private LineChartData data;
//    private LineChartView chart;

    private void generateData(String min,String max,List<PiontListBean> pointList,LineChartView chart) {

        List<Line> lines = new ArrayList<Line>();
        for (int i = 0; i < pointList.size(); ++i) {
            List<PointValue> values = new ArrayList<PointValue>();
            if(pointList.get(i).getPointList() != null){
            for(int j = 0; j < pointList.get(i).getPointList().size(); j++){
                PointBean bean = pointList.get(i).getPointList().get(j);
                String [] times = bean.getTime().split(":");
                int timepoint = Integer.parseInt(times[0]);
//                System.out.println("response==timepoint=======" + timepoint);
                PointValue point=new PointValue(timepoint,(int) Double.parseDouble(bean.getValue()));
                if("0".equals(bean.getIsSpecial())){

                }else{
                    point.setBig(true);
                }
                values.add(point);
            }
            Line line = new Line(values);
            line.setColor(ChartUtils.COLORS[i]);

            lines.add(line);

            }
        }

            data = new LineChartData(lines);

            List<AxisValue> mAxisValues = new ArrayList<AxisValue>();
            mAxisValues.add(new AxisValue(0).setLabel("00:00")); //为每个对应的i设置相应的label(显示在X轴)
            mAxisValues.add(new AxisValue(1).setLabel(""));
            mAxisValues.add(new AxisValue(2).setLabel(""));
            mAxisValues.add(new AxisValue(3).setLabel(""));
            mAxisValues.add(new AxisValue(4).setLabel(""));
            mAxisValues.add(new AxisValue(5).setLabel(""));
            mAxisValues.add(new AxisValue(6).setLabel("06:00"));
            mAxisValues.add(new AxisValue(7).setLabel(""));
            mAxisValues.add(new AxisValue(8).setLabel(""));
            mAxisValues.add(new AxisValue(9).setLabel(""));
            mAxisValues.add(new AxisValue(10).setLabel(""));
            mAxisValues.add(new AxisValue(11).setLabel(""));
            mAxisValues.add(new AxisValue(12).setLabel("12:00"));
            mAxisValues.add(new AxisValue(13).setLabel(""));
            mAxisValues.add(new AxisValue(14).setLabel(""));
            mAxisValues.add(new AxisValue(15).setLabel(""));
            mAxisValues.add(new AxisValue(16).setLabel(""));
            mAxisValues.add(new AxisValue(17).setLabel(""));
            mAxisValues.add(new AxisValue(18).setLabel("18:00"));
            mAxisValues.add(new AxisValue(19).setLabel(""));
            mAxisValues.add(new AxisValue(20).setLabel(""));
            mAxisValues.add(new AxisValue(21).setLabel(""));
            mAxisValues.add(new AxisValue(22).setLabel(""));
            mAxisValues.add(new AxisValue(23).setLabel(""));
            mAxisValues.add(new AxisValue(24).setLabel("24:00"));





            Axis axisX = new Axis(mAxisValues);

            List<AxisValue> yAxisValues = new ArrayList<AxisValue>();
//            yAxisValues.add(new AxisValue(2).setLabel("02")); //为每个对应的i设置相应的label(显示在X轴)
//            yAxisValues.add(new AxisValue(4).setLabel("04"));
//            yAxisValues.add(new AxisValue(6).setLabel("06"));
//            yAxisValues.add(new AxisValue(8).setLabel("08"));
//            yAxisValues.add(new AxisValue(10).setLabel("10"));
            int maxInt = (int) Double.parseDouble(max);
            int minInt = (int) Double.parseDouble(min);
            int interValue = (maxInt-minInt)/5;
            yAxisValues.add(new AxisValue(interValue).setLabel(interValue+"")); //为每个对应的i设置相应的label(显示在X轴)
            yAxisValues.add(new AxisValue(interValue * 2).setLabel((interValue * 2)+""));
            yAxisValues.add(new AxisValue(interValue * 3).setLabel((interValue * 3)+""));
            yAxisValues.add(new AxisValue(interValue * 4).setLabel((interValue * 4)+""));
            yAxisValues.add(new AxisValue(interValue * 5).setLabel((interValue * 5)+""));

            Axis axisY = new Axis(yAxisValues).setHasLines(true);

            data.setAxisXBottom(axisX);
            data.setAxisYLeft(axisY);

            chart.setLineChartData(data);

        final Viewport v = new Viewport(chart.getMaximumViewport());
        v.bottom = 0;
        v.top = (int) Double.parseDouble(max);
        v.left = 0;
        v.right = 30 - 1;
        chart.setMaximumViewport(v);
        chart.setCurrentViewport(v);

    }





}
